package cn.iocoder.yudao.module.shop.service.userholiday;

import cn.iocoder.yudao.module.shop.service.dayuser.ShopDayUserService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.userholiday.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.userholiday.ShopUserHolidayDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.userholiday.ShopUserHolidayConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.userholiday.ShopUserHolidayMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 艾灸师请假 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopUserHolidayServiceImpl implements ShopUserHolidayService {

    @Resource
    private ShopUserHolidayMapper userHolidayMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private ShopDayUserService shopDayUserService;

    @Override
    public Long createUserHoliday(ShopUserHolidayCreateReqVO createReqVO) {
        Long count = userHolidayMapper.selectCountByTime(createReqVO.getUserId(), createReqVO.getHolidayTime());
        if(count > 0){
            throw exception(USER_HOLIDAY_EXISTS_SAME_TIME);
        }

        // 插入
        ShopUserHolidayDO userHoliday = ShopUserHolidayConvert.INSTANCE.convert(createReqVO);

        AdminUserRespDTO userRespDTO = adminUserApi.getUser(createReqVO.getUserId());
        if(userRespDTO == null){
            throw exception(USER_HOLIDAY_USER_NOT_EXISTS);
        }

        userHoliday.setTenantId(userRespDTO.getTenantId());
        userHolidayMapper.insert(userHoliday);

        shopDayUserService.recordHoliday(createReqVO.getHolidayTime(), userRespDTO);

        // 返回
        return userHoliday.getId();
    }

    @Override
    public void updateUserHoliday(ShopUserHolidayUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserHolidayExists(updateReqVO.getId());
        // 更新
        ShopUserHolidayDO updateObj = ShopUserHolidayConvert.INSTANCE.convert(updateReqVO);
        userHolidayMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserHoliday(Long id) {
        // 校验存在
        validateUserHolidayExists(id);
        // 删除
        userHolidayMapper.deleteById(id);
    }

    private void validateUserHolidayExists(Long id) {
        if (userHolidayMapper.selectById(id) == null) {
            throw exception(USER_HOLIDAY_NOT_EXISTS);
        }
    }

    @Override
    public ShopUserHolidayDO getUserHoliday(Long id) {
        return userHolidayMapper.selectById(id);
    }

    @Override
    public List<ShopUserHolidayDO> getUserHolidayList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return userHolidayMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopUserHolidayDO> getUserHolidayPage(ShopUserHolidayPageReqVO pageReqVO) {
        return userHolidayMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopUserHolidayDO> getUserHolidayList(ShopUserHolidayExportReqVO exportReqVO) {
        return userHolidayMapper.selectList(exportReqVO);
    }

    @Override
    public List<ShopUserHolidayDO> getUserHolidayListByTime(LocalDateTime startTime, LocalDateTime endTime, Long tenantId){
        return userHolidayMapper.selectListByTime(startTime, endTime, tenantId);
    }

}
